//package io.renren.ssh;
//
//import com.alibaba.fastjson.JSONObject;
//import io.renren.common.utils.DateUtils;
//import io.renren.modules.systemconfig.entity.SystemConfigServersEntity;
//import io.renren.modules.systemconfig.service.SysConfigServerService;
//import lombok.extern.java.Log;
//import org.apache.sshd.client.session.ClientSession;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.context.ApplicationContext;
//import org.springframework.stereotype.Component;
//
//import javax.websocket.*;
//import javax.websocket.server.PathParam;
//import javax.websocket.server.ServerEndpoint;
//import java.io.IOException;
//import java.util.Date;
//import java.util.HashMap;
//
//@Component
//@ServerEndpoint("/websocket/{serverId}/{userId}")
//@Log
//public class test {
//    private static ApplicationContext applicationContext;
//    public static void setApplicationContext(ApplicationContext applicationContext) {
//        test.applicationContext = applicationContext;
//    }
//
//    private static Logger logger = LoggerFactory.getLogger(test.class);
//
//
//    public static HashMap<String,SshConnection> ServerId_SSHConn =new HashMap<>();
//
//    public static HashMap<String,String>   session_ServerId = new HashMap<>();
//    /**
//     * 连接建立成功调用的方法
//     */
//    @OnOpen
//    public void onOpen(Session session, @PathParam(value = "serverId") String serverId, @PathParam(value = "userId") String userId) {
//        try {
//            logger.info("建立连接");
//            if(!ServerId_SSHConn.containsKey(serverId+"-"+userId)){
//                initSSHConnect(serverId,serverId+"-"+userId);
//            }
//
//            SshResponse response = SshUtils.runCommand(ServerId_SSHConn.get(serverId+"-"+userId),"pwd");
//            if(response.getReturnCode()>0){
//                sendMessage(session, JSONObject.toJSONString(response));
//            }else{
//                String pwd = response.getStdOutput();
//                ServerId_SSHConn.get(serverId+"-"+userId).setPwd(pwd);
//                response = new SshResponse("登录成功!\n服务器时间："+ DateUtils.formatDefault(new Date()),"",0,pwd);
//                response.setPwd(ServerId_SSHConn.get(serverId+"-"+userId).getPwd());
//                sendMessage(session, JSONObject.toJSONString(response));
//                session_ServerId.put(session.getId(),serverId+"-"+userId);
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            SshResponse response = new SshResponse("服务器IO异常！","服务器IO异常！",1,"");
//            try{sendMessage(session, JSONObject.toJSONString(response));}catch (Exception e1){
//                e1.printStackTrace();
//            }
//        }
//    }
//
//    private void initSSHConnect(String serverId,String key) throws Exception{
//        SysConfigServerService sysConfigServerService=applicationContext.getBean(SysConfigServerService.class);
//        SystemConfigServersEntity se = sysConfigServerService.getById(serverId);
//        if(se!=null){
//            System.out.println("se.getLoginName()"+se.getLoginName());
//            System.out.println("se.getSshPwd()"+se.getSshPwd());
//            SshConnection  conn  = new SshConnection(se.getLoginName(),se.getSshPwd(),se.getIpAddr(),20000);
//            ClientSession session = SshUtils.SSH_login(conn);
//            conn.setSession(session);
//            ServerId_SSHConn.put(key,conn);
//        }else{
//            throw new SshTimeoutException("无法加载服务器信息!");
//        }
//    }
//
//    /**
//     * 连接关闭调用的方法
//     */
//    @OnClose
//    public void onClose() {
//        logger.info("连接关闭！");
//    }
//
//    /**
//     * 收到客户端消息后调用的方法
//     *
//     * @param message 客户端发送过来的消息
//     */
//    @OnMessage
//    public void onMessage(String message, Session session) {
//        logger.info("收到来自窗口 {}  的信息:{}" ,session.getId(), message);
//        try{
//            SshResponse response = SshUtils.runCommand(ServerId_SSHConn.get(session_ServerId.get(session.getId())),message);
//            sendMessage(session, JSONObject.toJSONString(response));
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//    }
//
//    /**
//     * @param session
//     * @param error
//     */
//    @OnError
//    public void onError(Session session, Throwable error) {
//        logger.error("发生错误");
//        error.printStackTrace();
//    }
//    public static void sendMessage(Session session,String message) throws IOException {
//        log.info(message);
//        session.getBasicRemote().sendText(message);
//    }
//}